package com.weilive.platform.third;

import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;

import com.weilive.core.netty.http.HttpServlet;
import com.weilive.platform.dao.app.mapper.AppChannelDO;

public abstract class ThirdPlatSDK {

	/**
	 * 检验用户登录
	 */
	public abstract ThirdCheck checkUserLogin(String uin, String token, String attach) throws Exception;

	/**
	 * 处理支付通知请求
	 */
	public abstract HttpResponse doPayNotify(HttpRequest request) throws Exception;

	/**
	 * 订单验证
	 */
	public abstract int verifyOrder(String order_cid, String receipt, String attach) throws Exception;

	/**
	 * 公共参数
	 */

	public static class ThirdCheck {
		public String userId;// 第三方用户id
		public String userToken;// 第三方使用的token

		public ThirdCheck(String userId, String userToken) {
			this.userId = userId;
			this.userToken = userToken;
		}
	}

	private String app_id; // 渠道appId
	private String app_key; // 渠道appKey
	private String app_secret; // 渠道appSecret
	private String app_other; // 渠道其他信息

	public void setChannelInfo(AppChannelDO channel) {
		app_id = channel.channel_app_id;
		app_key = channel.channel_app_key;
		app_secret = channel.channel_app_secret;
		app_other = channel.channel_app_other;
	}

	public String getAppId() {
		return app_id;
	}

	public String getAppKey() {
		return app_key;
	}

	public String getAppSecret() {
		return app_secret;
	}

	public String getAppOther() {
		return app_other;
	}

	public HttpServlet createPayNotifyServlet(String path) {
		return new ThirdPlatPayServlet(path, this);
	}

	/**
	 * 第三方支付平台支付通知HttpServlet
	 */

	public static class ThirdPlatPayServlet extends HttpServlet {

		private final String path;
		private final ThirdPlatSDK platSDK;

		public ThirdPlatPayServlet(String path, ThirdPlatSDK platSDK) {
			this.path = path;
			this.platSDK = platSDK;
		}

		@Override
		public String getServletPath() {
			return path;
		}

		@Override
		public HttpResponse processRequest(HttpRequest request) throws Exception {
			return platSDK.doPayNotify(request);
		}
	}
}
